import React, { Component } from 'react';
import PropTypes from 'prop-types';
import MaterialTable from 'material-table';
import ErrorPage from '../../../components/ErrorPage';
import Notifications from '../../../components/Notifications';
import DepartmentAPI from '../../../api/Departments';
import DeliveryContainersAPI from '../../../api/DeliveryContainers';
const generateStateData = (deliveryContainers, locations) => {
let locationLookupIds = {};
locationLookupIds[0] = 'None';
locationLookupIds = locations.slice(0).reduce((acc, location) => {
acc[location.locationId] = location.location;
return acc;
}, locationLookupIds);
let locationLookup = {};
locationLookup.None = 'None';
locationLookup = locations.slice(0).sort((a, b) => (a.location > b.location ? 1 : -1)).reduce((acc, location) => {
acc[location.location] = location.location;
return acc;
}, locationLookup);
const deliveryContainersData = deliveryContainers.map((deliveryContainer) => ({
...deliveryContainer,
locationId: deliveryContainer.locationId || 0,
locationName: locationLookupIds[deliveryContainer.locationId || 0],
}));
return {
deliveryContainersData,
locationLookup,
};
};
class DeliveryContainers extends Component {
static async getInitialProps({ authToken }) {
const departmentsApi = new DepartmentAPI(authToken);
const deliveryContainersApi = new DeliveryContainersAPI(authToken);
try {
const [locRes, deliveryContainerRes] = await Promise.all([
departmentsApi.getDepartments(),
deliveryContainersApi.getDeliveryContainers(),
]);
return {
deliveryContainers: deliveryContainerRes.data,
locations: locRes.data,
};
} catch (err) {
return {
locations: [],
deliveryContainers: [],
error: true,
errorMessage: 'Error loading data.',
};
}
}
static propTypes = {